package com.orchard.orchardfarmmanagementsystem.controller;


import com.orchard.orchardfarmmanagementsystem.dto.InventoryInfoDTO;
import com.orchard.orchardfarmmanagementsystem.dto.ResourceOptionDTO;
import com.orchard.orchardfarmmanagementsystem.service.InventoryService;
import com.orchard.orchardfarmmanagementsystem.service.SuppliesService;
import com.orchard.orchardfarmmanagementsystem.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/api")
public class InventoryController {


    @Autowired
    private InventoryService inventoryService;

    @GetMapping("/inventory")
    @PreAuthorize("hasAuthority('PERM_READ_DATA')")
    public Result<List<InventoryInfoDTO>> getInventory() {
        try {
            List<InventoryInfoDTO> inventoryInfoDTOList = inventoryService.getAllMaterials();
            return Result.success("获取库存信息成功", inventoryInfoDTOList);
        } catch (Exception e) {
            return Result.error(50001, "获取库存信息失败");
        }
    }

    @PostMapping("/inventory")
    @PreAuthorize("hasAuthority('PERM_ADMIN_DASHBOARD')")
    public Result addInventoryItem(@RequestBody ResourceOptionDTO resourceOptionDTO) {
        try {
            inventoryService.updateMaterial(resourceOptionDTO);
            return Result.success("新增库存成功");
        } catch (Exception e) {
            return Result.error(50001, "新增库存失败：" + e.getMessage());
        }
    }
    @GetMapping("/resource-options")
    @PreAuthorize("hasAuthority('PERM_READ_DATA')")
    public Result<List<ResourceOptionDTO>> getResourceOptions() {
        try {
            List<ResourceOptionDTO> options = inventoryService.getResourceOptions();
            return Result.success("获取资源选项成功", options);
        } catch (Exception e) {
            return Result.error(50003, "获取资源选项失败");
        }
    }
}
